﻿
#include "zyCtrol.h"

BlackMoonUI::zyCtrol::zyCtrol()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyCtrol_WndProc);
	m_obj->setChildWndProc(zyCtrol_ChildWndProc);
	m_obj->setDestroy(zyCtrol_Destroy);
}
BlackMoonUI::zyCtrol::~zyCtrol()
{
	
}
void WINAPI BlackMoonUI::zyCtrol::zyCtrol_Destroy(zyObject* obj)
{
	zyCtrol* zyct = (zyCtrol*)obj->getPtrData(L"this");
	zyct -> OnNCDestroy();
	zyWindow::zyWindow_Destroy(obj);
}
void BlackMoonUI::zyCtrol::OnNCDestroy()
{
	HGDIOBJ hRgn = (HGDIOBJ)GetMember(L"WindowhRgn");
	if (hRgn != 0) {
		DeleteObject(hRgn);
		SetMember(L"WindowhRgn", 0);
	}
}
LRESULT WINAPI BlackMoonUI::zyCtrol::zyCtrol_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	//std::cout << "zyCtrol_WndProc=uMsg: "<< uMsg << std::endl;
	if (uMsg == WM_NCDESTROY) {
		zyCtrol* zyct = (zyCtrol*)obj->getPtrData(L"this");
		zyct->OnNCDestroy();
	}
	return zyWindow::zyWindow_WndProc(obj,hWnd, uMsg, wParam, lParam, bHandled);
}
LRESULT WINAPI BlackMoonUI::zyCtrol::zyCtrol_ChildWndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	return zyBase::zyBase_ChildWndProc(obj,hWnd, uMsg, wParam, lParam, bHandled);
}
LRESULT WINAPI BlackMoonUI::zyCtrol::_MDI客户区窗口过程(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_NCCREATE) {
		CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
		if (cs && cs->lpCreateParams) {
			zyObject* this_ = reinterpret_cast<zyObject*>(cs->lpCreateParams);
			if (this_ != nullptr) {
				this_->setPtrData(L"hWnd", (void*)hWnd);
				zyEnv::getInstance().registerWnd(hWnd, this_);
			}
		}
	}
	zyObject* Ptr = zyEnv::getInstance().getWndObject(hWnd);
	if (Ptr != nullptr) {
		WNDPROC MDIClientWndProp = (WNDPROC)Ptr->getPtrData(L"oldWndProp");
		if (MDIClientWndProp != 0) {
			BOOL bHand = false;
			LRESULT ret = Ptr->callWndProc(hWnd, message, wParam, lParam, &bHand);
			if (bHand) {
				return ret;
			}
			return CallWindowProc(MDIClientWndProp, hWnd, message, wParam, lParam);
		}
	}
	return DefWindowProc(hWnd, message, wParam, lParam);//如果没有找到 zyBase 实例或消息未被处理，则调用默认窗口过程
}
BOOL BlackMoonUI::zyCtrol::_CreateMDIClient(HWND parent, const std::wstring& windowName, POINT pos, POINT size, int id, 
	DWORD style, DWORD exStyle, const std::wstring& className)
{
	HWND hWnd = getHwnd();
	if (hWnd)
		return false;
	WNDCLASSEX clsInfo = { 0 };
	//CLIENTCREATESTRUCT ccs={0};
	clsInfo.cbSize = sizeof(WNDCLASSEX);
	if (GetClassInfoEx(zyEnv::getInstance().getHandle(), L"MDIClient", &clsInfo) == 0)
		return false;
	SetMember(L"oldWndProp", (LONG_PTR)clsInfo.lpfnWndProc);
	clsInfo.lpszClassName = className.c_str();
	clsInfo.lpfnWndProc = zyCtrol::_MDI客户区窗口过程;
	RegisterClassEx(&clsInfo);

	//ccs.hWindowMenu = (HANDLE)GetMember (L"MDIClientMenu");
	//ccs.idFirstChild = (UINT)GetMember (L"MDIClientFirstMenuId");

	zyEnv::getInstance().dpiConvert(pos);
	zyEnv::getInstance().dpiConvert(size);
	_设置控件边框(getBorder(), style, exStyle);

	HWND hwnd_ = CreateWindowEx(exStyle, className.c_str(), windowName.c_str(), style | WS_CHILD | WS_CLIPSIBLINGS, pos.x, pos.y, size.x, size.y, parent, (HMENU)(UINT_PTR)id,
		zyEnv::getInstance().getHandle(), (LPVOID)this);

	return hwnd_ != 0;
}

BOOL BlackMoonUI::zyCtrol::_CreateCtrol(HWND parent, const std::wstring& windowName, POINT pos, POINT size, int id, 
	DWORD style, DWORD exStyle, const std::wstring& className)
{
	HWND hWnd = getHwnd();
	if (hWnd)
		return false;
	if (this == nullptr) {
		return FALSE; // 确保 this 指针有效
	}
	zyEnv::getInstance().dpiConvert(pos);
	zyEnv::getInstance().dpiConvert(size);
	_设置控件边框(getBorder(), style, exStyle);
	//WS_CLIPSIBLINGS风格会导致编辑框关联了调节器的时候，调节器显示不正常问题
	HWND hWnd1 = CreateWindowEx(exStyle, className.c_str(), windowName.c_str(), style | WS_CHILD | WS_VISIBLE ,/*| WS_CLIPSIBLINGS*/ pos.x, pos.y, size.x, size.y, parent, (HMENU)(UINT_PTR)id,
		zyEnv::getInstance().getHandle(), (LPVOID)this);
	if (_BindWindow(hWnd1) == false)
		return false;
	SendMessage(hWnd1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
	return hWnd1!=0;
}

BOOL BlackMoonUI::zyCtrol::_CreateWindow(HWND parent, const std::wstring& windowName, POINT pos, POINT size, int id, 
	DWORD style, DWORD exStyle, const std::wstring& className)
{
	HWND hWnd = getHwnd();
	if (hWnd)
		return false;

	zyEnv::getInstance().dpiConvert(pos);
	zyEnv::getInstance().dpiConvert(size);
	_设置控件边框(getBorder(), style, exStyle);
	HWND hwnd_ = CreateWindowEx(exStyle, className.c_str(), windowName.c_str(), style | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, pos.x, pos.y, size.x, 
		size.y, parent, (HMENU)(UINT_PTR)id, zyEnv::getInstance().getHandle(), getObject());
	SendMessage(hwnd_, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
	return hwnd_ != 0;
}

BOOL BlackMoonUI::zyCtrol::绑定(HWND 参数_父窗口, int 参数_资源ID, BOOL 参数_使用默认字体) {//成功返回真，失败返回假。

	HWND hwnd = GetDlgItem(参数_父窗口, 参数_资源ID);
	if (参数_使用默认字体 && hwnd != 0)
		SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 1);
	return _BindWindow(hwnd);
}
int BlackMoonUI::zyCtrol::GetID() {
	return (int)GetWindowLongPtr(getHwnd(), GWL_ID);
}